package com.iwhalecloud.bss.kite.cucc.service.pos.handler.impl;

import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosGroupDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosUserDTO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.service.pos.handler.IKitePosSceneHandler;
import com.iwhalecloud.bss.kite.cucc.service.pos.handler.KitePosHandlerContext;
import com.iwhalecloud.bss.kite.cucc.service.util.ChangeTypeUtils;
import com.iwhalecloud.bss.kite.cucc.service.util.KiteDcPublicUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Jonathan
 */
@Service
public class KitePosGroupsHandler implements IKitePosSceneHandler {
    @Override
    public void handle(KitePosHandlerContext context) {
        //从dc中获取配置，判断当前是否是归集集团创建操作
        /*String cateId = DcPublicCache.getCodea("20210223001", "CATE_ID");
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(context.getSceneInstId());
        String serviceOfferId = kiteSceneInst.getServiceOfferId();
        String rootInstId = kiteSceneInst.getRootInstId();
        String offerId = querySceneInstService.getOfferInst(context.getCustId(), rootInstId).getOfferId();
        String objId = OfferCache.get(offerId).getAttrValue("cateId");*/
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(context.getSceneInstId());
        String serviceOfferId = kiteSceneInst.getServiceOfferId();
        List<KitePosGroupDTO> list = new ArrayList<>();
        if (KiteDcPublicUtils.isCollectionGroup(context.getSceneInstId())) {
            if (KiteStringUtils.equalsAny(kiteSceneInst.getServiceOfferId(), LocalServiceOfferConsts.SERVICE_OFFER_COLLECTION_GROUP_ADD)) {
                List<KitePosUserDTO> users = (List<KitePosUserDTO>) querySceneInstService.getExtParameterValue(context.getSceneInstId(), "serialNumber");
                String business_number = querySceneInstService
                    .getCommonAttrValue(context.getSceneInstId(), "business_number");
                String primaryUserId = users.stream().filter(user -> business_number.equals(user.getSerialNumber()))
                    .findAny().map(KitePosUserDTO::getUserId).orElse("");
                if (KiteStringUtils.isEmpty(primaryUserId)) {
                    primaryUserId = querySceneInstService
                        .getCommonAttrValue(context.getSceneInstId(), "PRIMARY_USER_ID");
                }
                KitePosGroupDTO group = new KitePosGroupDTO();
                group.setGroupUserId(primaryUserId);
                group.setGroupName(
                    querySceneInstService.getCommonAttrValue(context.getSceneInstId(), "group_user_name"));
                group.setGroupTypeId("28");
                if (KiteStringUtils.equals(ServiceOfferConsts.SERVICE_OFFER_ORDER, serviceOfferId)) {
                    group.setChangeType(ChangeTypeUtils.CHANGE_TYPE_A);
                }
                else if (KiteStringUtils.equals("1006", serviceOfferId)) {
                    group.setChangeType(ChangeTypeUtils.CHANGE_TYPE_K);
                }
                else {
                    group.setChangeType(ChangeTypeUtils.CHANGE_TYPE_K);
                }
                list.add(group);
                context.getPosDto().setGroups(list);
            }
            else if (KiteStringUtils.equals(serviceOfferId, LocalServiceOfferConsts.SERVICE_OFFER_COLLECTION_GROUP_DEL)) {
                List<Map<String, Object>> groupUserInfos = querySceneInstService
                    .getExtParameterValue(context.getSceneInstId(), "delGroupUserInfos");
                for (Map<String, Object> map : groupUserInfos) {
                    KitePosGroupDTO group = new KitePosGroupDTO();
                    group.setGroupUserId(KiteMapUtils.getString(map, "PRIMARY_USER_ID"));
                    group.setGroupName(
                        querySceneInstService.getCommonAttrValue(context.getSceneInstId(), "PRIMARY_USER_NAME"));
                    group.setGroupTypeId("28");
                    group.setChangeType(ChangeTypeUtils.CHANGE_TYPE_K);
                    list.add(group);
                }
            }
            else if (!KiteStringUtils.equals(serviceOfferId, LocalServiceOfferConsts.SERVICE_OFFER_XIAOHU)) {
                List<KitePosUserDTO> kitePosUserDTOS = context.getPosDto().getUsers();
                if (KiteListUtils.isNotEmpty(kitePosUserDTOS)) {
                    KitePosUserDTO kitePosUserDTO = kitePosUserDTOS.get(0);
                    KitePosGroupDTO group = new KitePosGroupDTO();
                    group.setGroupUserId(kitePosUserDTO.getUserId());
                    group.setGroupName(querySceneInstService.getCommonAttrValue(context.getSceneInstId(), "group_user_name"));
                    group.setGroupTypeId("28");
                    if (KiteStringUtils.equals(ServiceOfferConsts.SERVICE_OFFER_ORDER, serviceOfferId)) {
                        group.setChangeType(ChangeTypeUtils.CHANGE_TYPE_A);
                    }
                    else if (KiteStringUtils.equals("1006", serviceOfferId)) {
                        group.setChangeType(ChangeTypeUtils.CHANGE_TYPE_D);
                    }
                    else {
                        group.setChangeType(ChangeTypeUtils.CHANGE_TYPE_K);
                    }
                    list.add(group);
                }
            }

            context.getPosDto().setGroups(list);
        }
    }
}
